package com.suixun.queue;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.suixun.lib.httpsqs4j.Httpsqs4j;
import com.suixun.lib.httpsqs4j.HttpsqsClient;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import cn.jpush.api.push.model.notification.PlatformNotification;

public class BaseDeal {

	protected static Logger logger = LoggerFactory.getLogger(BaseDeal.class);
	private static String mqshost;
	private static int mqsport;
	private static String mqsauth;
	public static HttpsqsClient client;
	static {
		Properties prop = new Properties();
		String path = BaseDeal.class.getClassLoader().getResource("webconfig.properties").getPath();
		try {
			InputStream is = new FileInputStream(path);
			prop.load(is);
			mqshost = prop.getProperty("mqs.host").trim();
			mqsport = Integer.valueOf(prop.getProperty("mqs.port").trim());
			mqsauth = prop.getProperty("mqs.auth").trim();

			Httpsqs4j.setConnectionInfo(mqshost, mqsport, mqsauth, "UTF-8");
			client = Httpsqs4j.createNewClient();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}

	public void JiGuangPush(List<String> telephones, String title, String content, boolean apnsProduction,
			boolean isOnlyAndroid, Long validTime, JPushClient jpushClient, String timingpic) {

		if (validTime == null) {
			validTime = 86400L;
		}
		Date now = new Date();
		int nowHours = now.getHours();
		PushPayload payload = null;

		Map<String, String> extras = new HashMap<String, String>();
		if (title == null)
			title = "";
		if (content == null)
			content = "";
		extras.put("title", title);
		extras.put("content", content);
		// 考勤图片
		extras.put("timingpic", timingpic);

		// 设置消息通知IOS
		PlatformNotification iosNotification = IosNotification.newBuilder().setBadge(1).setAlert(content)
				.setSound("sound.caf").addExtras(extras).build();
		// 在上午9点到下午9点IOS推送的消息，带声音;其他时间消息静音
		if (!(nowHours >= 9 && nowHours <= 21))
			iosNotification = IosNotification.newBuilder().autoBadge().setAlert(content).addExtras(extras).build();

		Notification notification = Notification.newBuilder().addPlatformNotification(iosNotification)
				// ios推送弹窗
				.addPlatformNotification(AndroidNotification.newBuilder().setAlert("").addExtras(extras).build())// android推送
				.build();

		Platform platform = Platform.android_ios();// 推送设备 为ios根android
		if (isOnlyAndroid) {
			platform = Platform.android();// 推送设备仅仅为android
			notification = Notification.newBuilder()
					.addPlatformNotification(AndroidNotification.newBuilder().setAlert("").addExtras(extras).build())// android推送
					.build();
		}
		payload = PushPayload.newBuilder().setNotification(notification).setPlatform(platform)
				.setOptions(Options.newBuilder().setApnsProduction(apnsProduction).setTimeToLive(validTime).build())// 推送附加选项
				// ApnsProduction=false测试环境
				// 用户不在线时86400秒内上线后，继续推送
				.setAudience(Audience.alias(telephones))// 推送目标
				.build();

		try {
			PushResult result = jpushClient.sendPush(payload);
			// System.out.println(result);
		} catch (APIConnectionException e) {
			logger.error("Connection error, should retry later", e);
			// e.printStackTrace();
		} catch (APIRequestException e) {
			// logger.error("Should review the error, and fix the request", e);
			// logger.info("HTTP Status: " + e.getStatus());
			// logger.info("Error Code: " + e.getErrorCode());
			// logger.info("Error Message: " + e.getErrorMessage());
			// e.printStackTrace();
		}
	}

	public void JiGuangPushAll(String title, String content, boolean apnsProduction, boolean isOnlyAndroid,
			Long validTime, JPushClient jpushClient, String timingpic) {

		if (validTime == null) {
			validTime = 86400L;
		}
		Date now = new Date();
		int nowHours = now.getHours();
		PushPayload payload = null;

		Map<String, String> extras = new HashMap<String, String>();
		if (title == null)
			title = "";
		if (content == null)
			content = "";
		extras.put("title", title);
		extras.put("content", content);
		// 考勤图片
		extras.put("timingpic", timingpic);

		// 设置消息通知IOS
		PlatformNotification iosNotification = IosNotification.newBuilder().setBadge(1).setAlert(content)
				.setSound("sound.caf").addExtras(extras).build();
		// 在上午9点到下午9点IOS推送的消息，带声音;其他时间消息静音
		if (!(nowHours >= 9 && nowHours <= 21))
			iosNotification = IosNotification.newBuilder().autoBadge().setAlert(content).addExtras(extras).build();

		Notification notification = Notification.newBuilder().addPlatformNotification(iosNotification)
				// ios推送弹窗
				.addPlatformNotification(AndroidNotification.newBuilder().setAlert("").addExtras(extras).build())// android推送
				.build();

		Platform platform = Platform.android_ios();// 推送设备 为ios根android
		if (isOnlyAndroid) {
			platform = Platform.android();// 推送设备仅仅为android
			notification = Notification.newBuilder()
					.addPlatformNotification(AndroidNotification.newBuilder().setAlert("").addExtras(extras).build())// android推送
					.build();
		}
		payload = PushPayload.newBuilder().setNotification(notification).setPlatform(platform)
				.setOptions(Options.newBuilder().setApnsProduction(apnsProduction).setTimeToLive(validTime).build())// 推送附加选项
				// ApnsProduction=false测试环境
				// 用户不在线时86400秒内上线后，继续推送
				.setAudience(Audience.all())// 推送目标
				.build();

		try {
			PushResult result = jpushClient.sendPush(payload);
			// System.out.println(result);
		} catch (APIConnectionException e) {
			logger.error("Connection error, should retry later", e);
			// e.printStackTrace();
		} catch (APIRequestException e) {
			// logger.error("Should review the error, and fix the request", e);
			// logger.info("HTTP Status: " + e.getStatus());
			// logger.info("Error Code: " + e.getErrorCode());
			// logger.info("Error Message: " + e.getErrorMessage());
			// e.printStackTrace();
		}
	}
	
	
	
	
	
	
	public void JiGuangSendPush(List<String> telephones, String title, String content, boolean apnsProduction,
			boolean isOnlyAndroid, Long validTime, JPushClient jpushClient, String otherValue, String model, String time) {

		if (validTime == null) {
			validTime = 86400L;
		}
		Date now = new Date();
		int nowHours = now.getHours();
		PushPayload payload = null;

		Map<String, String> extras = new HashMap<String, String>();
		if (title == null)
			title = "";
		if (content == null)
			content = "";
		extras.put("title", title);
		extras.put("content", content);
		extras.put("model", model);
		extras.put("otherValue", otherValue);
		extras.put("time", time);

		// 设置消息通知IOS
		PlatformNotification iosNotification = IosNotification.newBuilder().setBadge(1).setAlert(content)
				.setSound("sound.caf").addExtras(extras).build();
		// 在上午9点到下午9点IOS推送的消息，带声音;其他时间消息静音
		if (!(nowHours >= 9 && nowHours <= 21))
			iosNotification = IosNotification.newBuilder().autoBadge().setAlert(content).addExtras(extras).build();

		Notification notification = Notification.newBuilder().addPlatformNotification(iosNotification)
				// ios推送弹窗
				.addPlatformNotification(AndroidNotification.newBuilder().setAlert("").addExtras(extras).build())// android推送
				.build();

		Platform platform = Platform.android_ios();// 推送设备 为ios根android
		if (isOnlyAndroid) {
			platform = Platform.android();// 推送设备仅仅为android
			notification = Notification.newBuilder()
					.addPlatformNotification(AndroidNotification.newBuilder().setAlert("").addExtras(extras).build())// android推送
					.build();
		}
		payload = PushPayload.newBuilder().setNotification(notification).setPlatform(platform)
				.setOptions(Options.newBuilder().setApnsProduction(apnsProduction).setTimeToLive(validTime).build())// 推送附加选项
				// ApnsProduction=false测试环境
				// 用户不在线时86400秒内上线后，继续推送
				.setAudience(Audience.alias(telephones))// 推送目标
				.build();

		try {
			PushResult result = jpushClient.sendPush(payload);
			// System.out.println(result);
		} catch (APIConnectionException e) {
			logger.error("Connection error, should retry later", e);
			// e.printStackTrace();
		} catch (APIRequestException e) {
			// logger.error("Should review the error, and fix the request", e);
			// logger.info("HTTP Status: " + e.getStatus());
			// logger.info("Error Code: " + e.getErrorCode());
			// logger.info("Error Message: " + e.getErrorMessage());
			// e.printStackTrace();
		}
	}
}
